package com.appspot.bs24event.service;

import org.slim3.datastore.Datastore;

import com.appspot.bs24event.meta.GyojitterMeta;
import com.appspot.bs24event.model.Gyojitter;
import com.google.appengine.api.datastore.Key;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;


public class GyojitterService {

    private GyojitterMeta meta = GyojitterMeta.get();

    // AccessTokenを取得
    public Gyojitter getAccessToken(Key key) {
        return Datastore.get(meta,key);
    }

    // Twitterインスタンスの取得
    private Twitter getTwitter() {
        Object key = "agliczI0ZXZlbnRyEAsSCUd5b2ppdHRlchjcBAw";
        TwitterFactory factory = new TwitterFactory();
        GyojitterMeta gyojitterMeta = new GyojitterMeta();
        com.google.appengine.api.datastore.Entity entity = gyojitterMeta.modelToEntity(key);
        AccessToken at = new AccessToken((String)entity.getProperty("accessToken"),(String)entity.getProperty("accessTokenSecret"));
        // AccessTokenによる認証
        return factory.getInstance(at);
    }

    // つぶやく
    public void tweet(String message) throws TwitterException {
        Twitter twitter = getTwitter();
        twitter.updateStatus(message);
    }
}